<?
class Tbl_album_gambar extends CI_Model { 
    var $tabel;

     function __construct(){
         // Call the Model constructor
         parent::__construct();
		 $this->table = 'tbl_album_gambar';
     }
	
	function load_album() {
	$posisi = $this->uri->segment(3);
    if(!is_numeric($posisi))
    {
      $posisi='0';
      $query=$this->db->query("SELECT * FROM ".$this->table." ORDER BY id ASC LIMIT $posisi,10");
      return $query->result();
    }
	if(empty($posisi)) {$posisi = '0';}
  
		$query=$this->db->query("SELECT * FROM ".$this->table." ORDER BY id ASC LIMIT $posisi,10");
		return $query->result();	
	}
	
	function load_album_guest() {
	$posisi = $this->uri->segment(3);
    if(!is_numeric($posisi))
    {
      $posisi='0';
      $query=$this->db->query("SELECT *,tbl_album_gambar.id AS albumId, (SELECT COUNT(*) FROM tbl_foto WHERE album_id = albumId) AS totalFoto FROM tbl_album_gambar WHERE id>0 ORDER BY id ASC LIMIT $posisi,7");
      return $query->result();
    }
	if(empty($posisi)) {$posisi = '0';}
  
		$query=$this->db->query("SELECT *,tbl_album_gambar.id AS albumId, (SELECT COUNT(*) FROM tbl_foto WHERE album_id = albumId) AS totalFoto FROM tbl_album_gambar WHERE id>0 ORDER BY id ASC LIMIT $posisi,7");
		return $query->result();	
	}

	
	
	function hitung_album(){
		$data = $this->db->query("SELECT * FROM ".$this->table);
		$banyak= $this->db->affected_rows($data);
		return $banyak;
	}
	
	function hitung_album_guest(){
		$data = $this->db->query("SELECT * FROM ".$this->table." WHERE id > 0");
		$banyak= $this->db->affected_rows($data);
		return $banyak;
	}
	
	function add_album($nama, $deskripsi, $cover){

		$array = array('nama_album'	    => $nama,
					   'deskripsi' 		=> $deskripsi,
					   'cover' 	 		=> $cover
					   );
		$this->db->insert($this->table, $array);
	 }
	
	function delete_album($id) {
	$this->db->delete($this->table, array('id' => $id));
	}
	
	function delete_foto_where_album($id){
		$this->db->query("DELETE FROM tbl_foto WHERE album_id=$id");
	}
	
	function load_foto_where_album($id) {
	$query=$this->db->query("SELECT * FROM tbl_foto WHERE album_id=$id");
	return $query->result();
	}
	
	function update_album($id, $nama, $deskripsi){
		$this->db->query("UPDATE ".$this->table." SET
						nama_album = '$nama',
						deskripsi 	= '$deskripsi'					   
					    WHERE id=$id");
		
	 }
	
	function load_album_cover($id) {
	$query=$this->db->query("SELECT * FROM ".$this->table." WHERE id=$id");
	return $query->result();
	}
	
	function searchAlbumGambarGuest($key){
		$query = $this->db->query("SELECT * FROM ".$this->table." WHERE nama_album RLIKE '$key' OR deskripsi RLIKE '$key'");
		return $query->result();
	}
	
	function getLatestAlbumfoto(){
		$query=$this->db->query("SELECT * FROM ".$this->table." ORDER By RAND() LIMIT 3");
		return $query->result();
	}
	
}
?>